<template>
  <div>
    <Card>
      <Tables ref="tables" searchable search-place="top" :loading="listLoading" :columns="columns" v-model="list">
        <template slot="handleButton">
          <Button v-if="groupTypeManager_btn_add" type="primary" @click="handleAdd"><Icon type="md-add" />新增</Button>
        </template>
        <template slot="searchButton">
          <div style="float:right">
            <Input placeholder="类型名称" class="search-input" v-model="listQuery.name">
            <Button @click="getList" slot="append" type="info" icon="ios-search"></Button>
            </Input>
          </div>
        </template>
        <template slot="action" slot-scope="scope">
          <Divider type="vertical" />
          <Button v-if="groupTypeManager_btn_edit" type="primary" size="small" @click="handleEdit(scope.row)">编辑</Button>
          <Button v-if="groupTypeManager_btn_del" type="primary" size="small" @click="handleDel(scope.row)">删除</Button>
        </template>
      </Tables>
      <div style="margin: 10px;overflow: hidden">
        <div style="float: left;">
          <Page v-show="!listLoading" :total="total" :page-size-opts="[50,100,500,1000,2000]" :current="listQuery.page" show-total show-sizer :page-size="listQuery.limit" @on-change="handleCurrentChange" @on-page-size-change="handleSizeChange"></Page>
        </div>
      </div>
      <Modal :title="textMap[dialogStatus]" v-model="dialogFormVisible" :mask-closable='false' :width="500">
        <Form ref="form" :model="form" :label-width="100" :rules="rules">
          <FormItem label="编码" prop="code">
            <Input v-model="form.code"/>
          </FormItem>
          <FormItem label="类型名称" prop="name">
            <Input v-model="form.name" placeholder="请输入类型名称"/>
          </FormItem>
          <FormItem label="描述" prop="description">
            <Input v-model="form.description" placeholder="请输入描述"/>
          </FormItem>
        </Form>
        <div slot="footer">
          <Button type="text" @click="cancel('form')">取消</Button>
          <Button type="primary" v-if="dialogStatus=='create'" @click="create('form')">确 定</Button>
          <Button type="primary" v-else @click="update('form')">确 定</Button>
        </div>
      </Modal>
    </Card>
  </div>
</template>
<script>
import Tables from '_c/tables'
import { mapGetters } from 'vuex'
import {
  page,
  addObj,
  getObj,
  delObj,
  putObj
} from '@/api/admin/groupType/index'
export default {
  name: 'groupType',
  components: {
    Tables
  },
  data () {
    return {
      groupTypeManager_btn_edit: false,
      groupTypeManager_btn_del: false,
      groupTypeManager_btn_add: false,
      listLoading: false,
      total: 0,
      list: [],
      listQuery: {
        page: 1,
        limit: 50,
        name: undefined
      },
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '编辑',
        create: '创建'
      },
      form: {
        code: undefined,
        name: undefined,
        description: undefined
      },
      rules: {
        code: [{
          required: true,
          message: '请输入编码',
          trigger: 'blur'
        }, {
          min: 3,
          max: 20,
          message: '长度在 3 到 20 个字符',
          trigger: 'blur'
        }],
        name: [{
          required: true,
          message: '请输入类型名称',
          trigger: 'blur'
        }, {
          min: 3,
          max: 20,
          message: '长度在 3 到 20 个字符',
          trigger: 'blur'
        }],
        description: [{
          required: true,
          message: '请输入描述',
          trigger: 'blur'
        }, {
          min: 3,
          max: 20,
          message: '长度在 3 到 20 个字符',
          trigger: 'blur'
        }]
      },
      columns: [
        { title: '编码', key: 'code' },
        { title: '类型名称', key: 'name' },
        { title: '描述', key: 'description' },
        { title: '操作',
          key: 'action',
          align: 'center',
          render: (h, params) => {
            return h(
              'div',
              this.$refs.tables.$scopedSlots.action(params)
            )
          }
        }
      ]
    }
  },
  computed: {
    ...mapGetters([
      'elements'
    ])
  },
  created () {
    this.groupTypeManager_btn_edit = this.elements['groupTypeManager:btn_edit']
    this.groupTypeManager_btn_del = this.elements['groupTypeManager:btn_del']
    this.groupTypeManager_btn_add = this.elements['groupTypeManager:btn_add']
  },
  methods: {
    create (formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          addObj(this.form).then(() => {
            this.dialogFormVisible = false
            this.getList()
            this.$Notice.success({
              title: '成功',
              desc: '创建成功'
            })
          })
        } else {
          return false
        }
      })
    },
    cancel (formName) {
      this.dialogFormVisible = false
      this.$refs[formName].resetFields()
    },
    update (formName) {
      const set = this.$refs
      set[formName].validate(valid => {
        if (valid) {
          this.dialogFormVisible = false
          this.form.password = undefined
          putObj(this.form.id, this.form).then(() => {
            this.dialogFormVisible = false
            this.getList()
            this.$Notice.success({
              title: '成功',
              desc: '修改成功'
            })
          })
        } else {
          return false
        }
      })
    },
    handleSizeChange (val) {
      this.listQuery.limit = val
      this.getList()
    },
    getList () {
      this.listLoading = true
      page(this.listQuery).then(response => {
        this.list = response.data.rows
        this.total = response.data.total
        this.listLoading = false
      })
    },
    handleCurrentChange (val) {
      this.listQuery.page = val
      this.getList()
    },
    handleEdit (row) {
      getObj(row.id)
        .then(response => {
          this.form = response.data
          this.dialogFormVisible = true
          this.dialogStatus = 'update'
        })
    },
    handleDel (row) {
      delObj(row.id)
        .then(() => {
          this.$Notice.success({
            title: '成功',
            desc: '删除成功'
          })
          this.getList()
        })
    },
    handleAdd () {
      this.resetTemp()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
    },
    resetTemp () {
      this.form = {
        code: undefined,
        name: undefined,
        description: undefined
      }
    }
  }
}
</script>
